<template>
  <div class="invite-wrapper">
    <div class="invite-btn">
      <button type="button" @click="toRegister" class="btn btn-large btn-yellow">我要去注册</button>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import qs from 'qs'

import Config from '@/config/config'
import util from '@/util/util'

import { mapGetters } from 'vuex'

export default {
  name: 'Invite',
  data () {
    return {
    }
  },
  created() {
    this.$toast({
      duration: 5000,
      message: '点击【右上角按钮】发送给朋友或者分享到朋友圈'
    })
    this.setShare()
  },
  computed: {
    ...mapGetters([
      'userInfo'
    ])
  },
  beforeCreate () {
    var webUrl = window.location.href
    var jsApiList = [
       "onMenuShareTimeline",
       "onMenuShareAppMessage",
       "onMenuShareQQ",
       "onMenuShareWeibo"
    ]
    if (util.isAndroid()) {
      var that = this
      setTimeout(function() {
        that.SDKRegister(that, webUrl, jsApiList)
      }, 500)
    }
  },
  methods: {
    toRegister: function() {
      var params = {}
      if (typeof(this.$route.query.invite) != 'undefined') {
        params = { invite: this.$route.query.invite, inviterId: this.$route.query.inviterId }
      }
      this.$router.push({ name: 'register', params: params })
    },
    setShare: function() {
      var that = this
      this.wx.ready((res) => {
        // 分享到朋友圈
        var link = Config.baseUrl + '?invite=true&inviterId=' + that.userInfo.id
        var title = that.userInfo.nickname + '邀请您注册书画点评网'
        // var imgUrl = that.userInfo.avatar
        var imgUrl = 'http://p2buepmu3.bkt.clouddn.com/works/2018/03/12/cb18b3197e1f4a63874beba400706f6d.jpg'
        var desc = ''
        that.wx.onMenuShareTimeline({
          title: title, // 分享标题
          link: link, // 分享链接
          imgUrl: imgUrl, // 分享图标
          success () {
            that.$toast.success('分享成功')
          },
          cancel () {
          }
        })
        // 分享给朋友
        that.wx.onMenuShareAppMessage({
          title: title, // 分享标题
          desc: desc, // 分享描述
          link: link, // 分享链接
          imgUrl: imgUrl, // 分享图标
          success: function () {
            that.$toast.success('分享成功')
          },
          cancel: function () {
          }
        })
      })
    
    }
  }
}

</script>

<style lang="scss">

@import '../../assets/style/const.scss';

.invite-wrapper {
  width: 100%;
  height: 100%;
  position: relative;
  background: url('../../assets/img/img_invite.jpg') no-repeat fixed;
  background-size: cover;
  .invite-btn {
    position: absolute;
    bottom: 80px;
    left: 5%;
    width: 90%;
  }
}
</style>
